﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using TheVerge.Base.Platform;
using TheVerge.Base;

namespace TheVerge.Base.Models
{
    public class VMSections : AVM, IPinnable
    {
        public VMHubs Hubs
        {
            get { return GetValue(() => Hubs); }
            set { SetValue(value, () => Hubs); }
        }

        public VMBrands Brands
        {
            get { return GetValue(() => Brands); }
            set { SetValue(value, () => Brands); }
        }

        public VMCategories Categories
        {
            get { return GetValue(() => Categories); }
            set { SetValue(value, () => Categories); }
        }

        public VMSections()
        {
            Brands = new VMBrands();
            Categories = new VMCategories();
            Hubs = new VMHubs();
        }

        public string DisplayName { get { return @"Sections"; } }

        public Uri Tile { get { return new Uri(@"/Images/Tiles/Sections.png", UriKind.Relative); } }

        public Dictionary<string, string> QueryStrings { get { return new Dictionary<string, string>(); } }

        public void LoadFromPin(Dictionary<string, string> queryStrings) { }
    }

    public abstract class VMSection<T> : AVM
    {
        private static T[] all;

        public abstract string DisplayName { get; }

        public VMSection()
        {
            if (all == null)
                LoadSection();
        }

        public bool IsLoading
        {
            get { return GetValue(() => IsLoading); }
            set { SetValue(value, () => IsLoading); }
        }

        private void LoadSection()
        {
            IsLoading = true;
            Load(results =>
            {
                if (results != null)
                    all = results;
                IsLoading = false;
                Notify(() => All);
            });
        }

        protected abstract void Load(Action<T[]> results);

        public T[] All { get { return all; } }
    }
}